<template>
  <el-dialog v-model="dialogVisible" :destroy-on-close="true" :align-center="true" width="50%" :title="`审核企业用户信息`">
    <el-form
      ref="ruleFormRef"
      :rules="rules"
      :disabled="dialogData.isView"
      :model="dialogData.itemInfo"
      label-width="100px"
      label-suffix=" :"
      :hide-required-asterisk="dialogData.isView"
    >
      <el-row :gutter="20">
        <!-- <el-col :span="24">
          <div class="info-title">审核信息</div>
        </el-col> -->
      </el-row>
      <el-row :gutter="20" class="mb15">
        <el-col :span="12">
          <el-form-item>
            <el-button type="primary" class="bigBtn" @click="handleAuditP">审核通过</el-button>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item>
            <el-input v-model="dialogData.itemInfo.auditContent" placeholder="请选择审核内容" maxlength="100" show-word-limit>
              <template #append> <el-button type="danger" @click="handleAuditF">审核不通过</el-button></template>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" v-show="!dialogData.isView" @click="handleSubmit" :loading="btnLoading">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { getOptTitle, OptEnum, sysEnum } from "@/biz/eenum/appEnum";
import { DialogParam } from "@/biz/interface/DialogType";
import { CompanyUserEntity } from "@/biz/interface/CompanyType";
import { getUserInfoApi } from "@/api/modules/sys/user";
import { companyUserAuditFApi, companyUserAuditPApi } from "@/api/modules/cpy/act";
import { UserInfoEntity } from "@/biz/interface/userType";

const rules = reactive({
  phone: [{ required: true, message: "手机号不能为空", trigger: "blur" }]
});

//页面的数据
interface UserDialogOption extends DialogParam {
  //弹出表单的标题
  title: string;
  //是否是查看状态，查看状态是不可以提交数据  isView: false,
  isView: boolean;
  //是否是审核状态
  isAudit: boolean;
  //当前的数据对象
  itemInfo: UserInfoEntity;
}

//提交按钮，避免多次提交
const btnLoading = ref(false);
// 弹出框的参数
const dialogVisible = ref(false);
const dialogData = reactive<UserDialogOption>({
  optType: OptEnum.UNKNOW,
  title: "",
  isView: false,
  isAudit: false,
  itemInfo: {
    account: "",
    password: "",
    name: "",
    gender: sysEnum.ONE,
    phone: "",
    idCard: "",
    email: "",
    avator: "",
    auditContent: ""
  }
});

// 接收父组件传过来的参数
const acceptParams = (params: DialogParam): void => {
  dialogData.optType = params.optType;
  dialogData.rowData = params.rowData;

  dialogData.isView = dialogData.optType === OptEnum.VIEW;
  dialogData.isAudit = dialogData.optType === OptEnum.AUDIT;
  dialogData.title = getOptTitle(dialogData.optType);

  if (dialogData.optType === OptEnum.CREATE) {
    initItemInfo();
  } else if (dialogData.rowData && dialogData.rowData.id) {
    getItemInfo(dialogData.rowData.id);
  }

  dialogVisible.value = true;
};

const initItemInfo = (): void => {
  dialogData.itemInfo = {
    account: "",
    password: "",
    name: "",
    gender: sysEnum.ONE,
    phone: "",
    idCard: "",
    email: "",
    avator: "",
    auditContent: ""
  };
};

//获取当前数据的详情，不通过表格上的数据行，直接调用接口获取数据
const getItemInfo = async (id: string) => {
  if (id) {
    //通过接口获取数据
    let { data } = await getUserInfoApi({ id });
    // console.log("接收到服务器端的数据", data);
    dialogData.itemInfo = data as unknown as CompanyUserEntity;
  }
};

//审核通过
const handleAuditP = async () => {
  let res = await companyUserAuditPApi({ id: dialogData.rowData!.id });
  if (res.data) {
    emits("refresh-list");
    ElMessage.success("审核通过");
  }
};
//审核不通过
const handleAuditF = async () => {
  let res = await companyUserAuditFApi({
    id: dialogData.rowData!.id,
    auditContent: dialogData.itemInfo.auditContent!
  });
  if (res.data) {
    emits("refresh-list");
    ElMessage.success("审核不通过");
  }
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  btnLoading.value = true;
  ruleFormRef.value!.validate(async valid => {
    if (!valid) {
      btnLoading.value = false;
      return;
    }
    try {
      emits("refresh-list");
      dialogVisible.value = false;
    } catch (error) {
      // console.log(error);
    } finally {
      btnLoading.value = false;
    }
  });
};

// // 公共校验方法（图片上传成功触发重新校验）
// const checkValidate = (val: string) => {
//   ruleFormRef.value!.validateField(val, () => {});
// };

//页面事件
const emits = defineEmits(["refresh-list"]);

//页面参数
defineExpose({
  acceptParams
});
</script>
<style scoped lang="scss">
.bigBtn {
  width: 200px;
  height: 60px;
  font-size: 1.5em;
  font-weight: 500;
}
</style>
